package com.xuetang.controller.admin;

import com.xuetang.entity.SysUser;
import com.xuetang.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 微信用户REST控制器
 */
@RestController
@RequestMapping("/admin/api/users")
public class WxUserController {

    @Autowired
    private UserService userService;

    /**
     * 获取所有微信用户
     */
    @GetMapping("/wx-users")
    public Map<String, Object> listWxUsers(
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) Integer diabetesType,
            @RequestParam(required = false) Integer status,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {

        Map<String, Object> result = new HashMap<>();
        try {
            List<SysUser> wxUsers = userService.listWxUsers(keyword, diabetesType, status, page, size);
            result.put("code", 0);
            result.put("message", "获取成功");
            result.put("data", wxUsers);
        } catch (Exception e) {
            result.put("code", 1);
            result.put("message", "获取失败：" + e.getMessage());
        }
        return result;
    }

    /**
     * 获取微信用户详情
     */
    @GetMapping("/wx-users/{id}")
    public Map<String, Object> getWxUser(@PathVariable Long id) {
        Map<String, Object> result = new HashMap<>();
        try {
            SysUser wxUser = userService.getById(id);
            if (wxUser != null) {
                result.put("code", 0);
                result.put("message", "获取成功");
                result.put("data", wxUser);
            } else {
                result.put("code", 1);
                result.put("message", "用户不存在");
            }
        } catch (Exception e) {
            result.put("code", 1);
            result.put("message", "获取失败：" + e.getMessage());
        }
        return result;
    }

    /**
     * 修改微信用户状态
     */
    @PutMapping("/wx-users/{id}/status")
    public Map<String, Object> updateWxUserStatus(@PathVariable Long id, @RequestBody Map<String, Object> params) {
        Map<String, Object> result = new HashMap<>();
        try {
            Integer status = (Integer) params.get("status");
            if (status == null) {
                result.put("code", 1);
                result.put("message", "状态参数不能为空");
                return result;
            }

            boolean success = userService.updateStatus(id, status);
            if (success) {
                result.put("code", 0);
                result.put("message", "修改成功");
            } else {
                result.put("code", 1);
                result.put("message", "修改失败");
            }
        } catch (Exception e) {
            result.put("code", 1);
            result.put("message", "修改失败：" + e.getMessage());
        }
        return result;
    }

    /**
     * 导出用户数据
     */
    @GetMapping("/export")
    public void exportUsers(
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) Integer diabetesType,
            @RequestParam(required = false) Integer status) {
        // 这里实际上应该实现导出功能，返回Excel或CSV文件
        // 由于涉及到文件下载，此处省略具体实现
    }
}